#define _CRT_SECURE_NO_DEPRECATE
#include <fstream>
#include <iostream>
#include "TgaBuffer.h"

namespace MASGK
{

	CTgaBuffer::CTgaBuffer(void)
	{
	}

	CTgaBuffer::CTgaBuffer(int _w, int _h)		// NOT WORKING HERE:  :m_Width(_w), m_Height(_h), m_Length(GetLength())   - due to:  "not a nonstatic data member or base class of class"
	{
		m_Width = _w;
		m_Height = _h;
		m_Length = GetLength();
		m_ColorBuf = new uint*[m_Width];
		m_DepthBuf = new float*[m_Width];

		// Allocation & initialization of color & depth buffers
		for (int i = 0; i < m_Width; i++)
		{
			m_ColorBuf[i] = new uint[m_Height];
			m_DepthBuf[i] = new float[m_Height];

			for (int j = 0; j < m_Height; j++)
			{
				m_ColorBuf[i][j] = 0x44434241;
				m_DepthBuf[i][j] = -9999999.0f;
			}
		}
	}
/*
	void CTgaBuffer::Save()
	{
		
		std::ofstream tgaFile("sample.tga", std::ios::binary);

		if (!tgaFile)
			return;

		// The image header
		unsigned char header[ 18 ] = { 0 };
		header[  2 ] = 1;						// truecolor
		header[ 12 ] =  m_Width - 1        & 0xFF;
		header[ 13 ] = ((m_Width - 1)  >> 8) & 0xFF;
		header[ 14 ] =  m_Height - 1       & 0xFF;
		header[ 15 ] = ((m_Height - 1) >> 8) & 0xFF;
		header[ 16 ] = 32;						// bits per pixel

		tgaFile.write( (const char*)header, 18 );

		// The image data is stored bottom-to-top, left-to-right
		for (int y = m_Height - 1; y >= 0; y--)
		{
			for (int x = 0; x < m_Width; x++)
			{
			//	char ch[4] = {(char) m_ColorBuf[x][y], (char) m_ColorBuf[x][y] >> 8, (char) m_ColorBuf[x][y] >> 16, (char) m_ColorBuf[x][y] >> 24};
				char ch[4] = {((char) m_ColorBuf[x][y] >> 24 & 0xFF), ((char) m_ColorBuf[x][y] >> 16 & 0xFF), ((char) m_ColorBuf[x][y] >> 8 & 0xFF), ((char) m_ColorBuf[x][y] & 0xFF)};
			//	std::cout << std::endl << (m_ColorBuf[x][y] >> 24 & 0xFF) << " " << (m_ColorBuf[x][y] >> 16 & 0xFF) << " " << (m_ColorBuf[x][y] >> 8 & 0xFF) << " " << (m_ColorBuf[x][y] & 0xFF);
				tgaFile.write((const char*) ch, 4);
				//tgaFile.put( (char) m_ColorBuf[x][y] );
			}
		}

		tgaFile.close();
		
	}
*/
	void CTgaBuffer::Save()
	{
	  FILE * pFile;
	  char header[] = 
				{
					0x00,			// 0
					0x00,			// 1
					0x02,			// 2
					0x00,			// 3
					0x00,			// 4
					0x00,			// 5
					0x00,			// 6
					0x00,			// 7
					0x00,			// 8
					0x00,			// 9
					0x00,			// 10
					0x00,			// 11
					0x00,			// 12
					0x01,			// 13
					0x00,			// 14
					0x01,			// 15
					0x20,			// 16
					0x00,			// 17
				};

	  pFile = fopen ("out.tga", "wb");
	  fwrite (header , sizeof(char), sizeof(header), pFile);

		for (int x = 0; x < m_Width; x++)
		{
			for (int y = 0; y < m_Height; y++)
			{
				char ch[4] = {(char) m_ColorBuf[x][y] & 0xFF, (char) (m_ColorBuf[x][y] >> 8)  & 0xFF, (char) (m_ColorBuf[x][y] >> 16)  & 0xFF, (char) (m_ColorBuf[x][y] >> 24) & 0xFF};
				fwrite (ch , sizeof(char), sizeof(ch), pFile);
			}
		}

	  fclose (pFile);
	}

	void CTgaBuffer::Save(int _width, int _height, COutImage& _img, const char* _fileName)
	{
	  uint** data = _img.GetData();
	  FILE * pFile;
	  char header[] = 
				{
					0x00,			// 0
					0x00,			// 1
					0x02,			// 2
					0x00,			// 3
					0x00,			// 4
					0x00,			// 5
					0x00,			// 6
					0x00,			// 7
					0x00,			// 8
					0x00,			// 9
					0x00,			// 10
					0x00,			// 11
					0x00,			// 12
					0x01,			// 13
					0x00,			// 14
					0x01,			// 15
					0x20,			// 16
					0x00,			// 17
				};

	  pFile = fopen (_fileName, "wb");
	  fwrite (header , sizeof(char), sizeof(header), pFile);

		for (int y = 0; y < m_Height; y++)
		{
			for (int x = 0; x < m_Width; x++)
			{
				char ch[4] = {(char) data[x][y] & 0xFF, (char) (data[x][y] >> 8)  & 0xFF, (char) (data[x][y] >> 16)  & 0xFF, (char) (data[x][y] >> 24) & 0xFF};
				fwrite (ch , sizeof(char), sizeof(ch), pFile);
			}
		}

	  fclose (pFile);
	}

	void CTgaBuffer::Save(int _width, int _height, const char* _fileName)
	{
	  uint** data = m_ColorBuf;
	  FILE * pFile;
	  char header[] = 
				{
					0x00,			// 0
					0x00,			// 1
					0x02,			// 2
					0x00,			// 3
					0x00,			// 4
					0x00,			// 5
					0x00,			// 6
					0x00,			// 7
					0x00,			// 8
					0x00,			// 9
					0x00,			// 10
					0x00,			// 11
					0x00,			// 12
					0x01,			// 13
					0x00,			// 14
					0x01,			// 15
					0x20,			// 16
					0x00,			// 17
				};

	  pFile = fopen (_fileName, "wb");
	  fwrite (header , sizeof(char), sizeof(header), pFile);

		for (int y = 0; y < m_Height; y++)
		{
			for (int x = m_Width - 1; x >=0; x--)
			{
				char ch[4] = {(char) data[x][y] & 0xFF, (char) (data[x][y] >> 8)  & 0xFF, (char) (data[x][y] >> 16)  & 0xFF, (char) (data[x][y] >> 24) & 0xFF};
				fwrite (ch , sizeof(char), sizeof(ch), pFile);
			}
		}

	  fclose (pFile);
	}

	CTgaBuffer::~CTgaBuffer(void)
	{
	}

	void CTgaBuffer::Clear(float3 _clearColor)
	{
		int r, g, b;
		uint color;

		r = (int) (_clearColor.r * 255.0f);
		g = (int) (_clearColor.g * 255.0f);
		b = (int) (_clearColor.b * 255.0f);

		if (r < 0)
			r = 0;
		else if (r > 255)
			r = 255;

		if (g < 0)
			g = 0;
		else if (g > 255)
			g = 255;

		if (b < 0)
			b = 0;
		else if (b > 255)
			b = 255;

		color = 0xFF;
        color <<= 8;
        color |= r;
        color <<= 8;
        color |= g;
        color <<= 8;
        color |= b;

		for (int i = 0; i < m_Width; i++)
		{
			m_ColorBuf[i] = new uint[m_Height];
			m_DepthBuf[i] = new float[m_Height];

			for (int j = 0; j < m_Height; j++)
			{
				m_ColorBuf[i][j] = color;
				m_DepthBuf[i][j] = -9999999.0f;
			}
		}		
	}
}